<?php
namespace common\models;

use yii\db\ActiveRecord;
use common\models\Tree;
use backend\components\Utools;

class District extends ActiveRecord
{

    /**
     * Category_priv类关联数据库表名称 
     */
    public static function tableName()
    {
        return '{{%district}}';
    }
    
    /**
    * 验证规则
    */
    public function rules()
    {
        return[
            [['siteid','dt_id','parentid'],'integer'],
            [['dt_name'],'required','message'=>'必填'],
            ['dt_name','unique','message'=>'名称已存在，请更换'],
            [['display','listorder','status','arrparentid'], 'default', 'value'=>'0'],
        ];
    }

    /**
     * 属性标签
    */
    public function attributeLabels()
    {
        return [
            'dt_id' => '区县ID',
            'dt_name'=>'区县名称',
            'parentid'=>'父级ID',
            'arrparentid'=>'父级ID数组',
            'arrchildid'=>'子级ID数组',
            'siteid'=>'站点ID',
            'display'=>'展示',
            'status'=>'状态',
            'listorder'=>'排序',
        ];
    }

    /**
     * aftersave
     */
    public function afterSave($insert,$changedAttributes)
    {
        if($insert || $changedAttributes)
        {
            if($this->parentid!=0)
            {
                $parent = self::find()->where('dt_id='.$this->parentid.' AND status=0')->one();
                $arrparentid='0';
                if($parent)
                {
                    $arrparentid = $parent->arrparentid.','.$this->parentid;
                }
                $this->arrparentid = $arrparentid;
                $this->save();
            }
        }
        if($this->status == '-1')
        {
           $this->delete_child($this->arrchildid);
        }
        return true;
    }
    
    /**
     * 展示 num2chinese
     */
    public static function num2chinese($display)
    {
        if($display == 0)
        {
            return '展示';
        }
        else
        {
            return '禁用';
        }
    }
    
    /**
     * 递归删除子栏目
     */
    protected function delete_child($childs)
    {
        if($childs)
        {
            $childs_arr = explode(',',$childs);
            foreach($childs_arr as $key=>$child)
            {
                
                $chi = District::find()->where('dt_id='.$child.' AND status=0')->one();
                $chi->status = '-1';
                $chi->save();
                if($chi->arrchildid)
                {
                    $this->delete_child($chi->arrchildid);
                }
            }
        }
        return true;
    }
    
    /**
     * 获取分类等级
     * @param $id
     * @param array $array
     * @param int $i
     * @return int
     */
    public static function get_level($id,$array=array(),$i=0)
    {
        foreach($array as $n=>$value){
            if($value['dt_id'] == $id)
            {
                if($value['parentid']== '0') return $i;
                $i++;
                return self::get_level($value['parentid'],$array,$i);
            }
        }

    }

    
    
}

?>